package com.dy.lock.screen.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.CustomerBlacklistComponent;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.jersey.api.EasemobIMUsers;
import com.dy.lock.screen.pojo.CustomerAttentionInfo;
import com.dy.lock.screen.pojo.CustomerBlacklistInfo;
import com.dy.lock.screen.pojo.CustomerMarkInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.CustomerAttentionService;
import com.dy.lock.screen.service.CustomerBlacklistService;
import com.dy.lock.screen.service.CustomerMarkRelationService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.vo.CustomerBlacklistVo;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;

@Component("customerBlacklistComponent")
public class CustomerBlacklistComponentImpl implements
		CustomerBlacklistComponent {

	private static final Logger log = Logger
			.getLogger(CustomerBlacklistComponentImpl.class);
	private Gson gson;

	@Resource(name = "customerBlacklistService")
	private CustomerBlacklistService customerBlacklistService;

	@Resource(name = "customerAttentionService")
	private CustomerAttentionService customerAttentionService;

	@Resource(name = "customerMarkRelationService")
	private CustomerMarkRelationService customerMarkRelationService;

	public CustomerBlacklistComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean joinBlacklist(String body, Head head) {
		CustomerBlacklistVo vo = gson.fromJson(body, CustomerBlacklistVo.class);
		if (vo != null) {
			CustomerBlacklistInfo blackInfo = new CustomerBlacklistInfo();
			blackInfo.setBlackCustomer(vo.getBlackCustomer());
			blackInfo.setBeBlackedCustomer(vo.getBeBlackedCustomer());
			int result = -1;
			CustomerBlacklistInfo black = this.customerBlacklistService
					.queryByBlackIdAndBeBlackId(blackInfo);
			if (black == null) {
				result = this.customerBlacklistService.save(blackInfo);
			} else {
				log.info("requestId:" + head.getRequestId() + "用户已经加入过黑名单。");
				throw new ServerDisponseException(
						TransmissionCode.CUSTOMER_JOIN_BLACKLIST.getCode(),
						TransmissionCode.CUSTOMER_JOIN_BLACKLIST.getMessage());
			}
			if (result > 0) {
				// 向环信平台发送加入黑名单请求
				String[] usernames = new String[1];
				usernames[0] = vo.getBeBlackedCustomer();
				ObjectNode addblacklistNode = EasemobIMUsers.addBlacklist(vo
						.getBlackCustomer(), usernames);
				if (addblacklistNode != null
						&& addblacklistNode.get("statusCode") != null
						&& addblacklistNode.get("statusCode").asInt() == 200) {
					log.info("requestId:" + head.getRequestId()
							+ "在环信用户体系中加入黑名单成功。" + addblacklistNode.toString());
				}
				// 取消关注者关注状态
				CustomerAttentionInfo att = new CustomerAttentionInfo();
				att.setAttentionId(vo.getBlackCustomer());
				att.setBeConcernedId(vo.getBeBlackedCustomer());
				this.customerAttentionService.delete(att);
				// 取消被关注者关注状态
				att.setAttentionId(vo.getBeBlackedCustomer());
				att.setBeConcernedId(vo.getBlackCustomer());
				this.customerAttentionService.delete(att);
				return true;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log
					.info("requestId:" + head.getRequestId()
							+ ",用户加入黑名单操作时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}

	}

	@Override
	public boolean remove(String body, Head head) {
		CustomerBlacklistVo vo = gson.fromJson(body, CustomerBlacklistVo.class);
		if (vo != null) {
			CustomerBlacklistInfo blackInfo = new CustomerBlacklistInfo();
			blackInfo.setBlackCustomer(vo.getBlackCustomer());
			blackInfo.setBeBlackedCustomer(vo.getBeBlackedCustomer());
			int result = this.customerBlacklistService.delete(blackInfo);
			if (result >= 0) {
				ObjectNode deleteblacklistNode = EasemobIMUsers
						.deleteBlacklist(vo.getBlackCustomer(), vo
								.getBeBlackedCustomer());
				if (deleteblacklistNode != null
						&& deleteblacklistNode.get("statusCode") != null
						&& deleteblacklistNode.get("statusCode").asInt() == 200) {
					log.info("requestId:" + head.getRequestId()
							+ "在环信用户体系中移除黑名单成功。"
							+ deleteblacklistNode.toString());
				}
				return true;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",用户移除名单操作时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}

	}

	@Override
	public List<CustomerBlacklistInfo> list(String body, Head head) {
		CustomerBlacklistVo vo = gson.fromJson(body, CustomerBlacklistVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> para = new HashMap<String, Object>();
			para.put("pageStart", pageStart);
			para.put("pageEnd", pageEnd);
			para.put("blackCustomer", vo.getBlackCustomer());
			List<CustomerBlacklistInfo> list = this.customerBlacklistService
					.queryPage(para);
			if (list != null && !list.isEmpty()) {
				for (CustomerBlacklistInfo info : list) {
					if (StringUtils.isBlank(info.getUserName())) {
						info.setUserName(info.getUserId());
					}
					if (StringUtils.isNoneBlank(info.getHeaderPath())) {
						info.setHeaderPath(CommonUtil
								.getUrlPrefixApi("photo.url.prefix.path.api")
								+ info.getHeaderPath());
					}
					List<CustomerMarkInfo> markInfos = this.customerMarkRelationService
							.queryListByCid(info.getUserId());
					info.setMarkInfoList(markInfos);
				}
				return list;
			} else {
				return new ArrayList<CustomerBlacklistInfo>();
			}
		} else {
			log
					.info("requestId:" + head.getRequestId()
							+ ",获取黑名单列表操作时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
